package com.zhizuobiao.controller;

import com.zhizuobiao.entity.Items;
import com.zhizuobiao.exception.MyException;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * day2内容
 */
@Controller
public class TestController {

    @RequestMapping("/test1")
    public String test1(String name, String qq, Model model, HttpServletRequest request) {

        System.out.println("test1");
//        request.getParameter("name");
        model.addAttribute("message", name + "_" + qq);
        return "simple";
    }

//    @ModelAttribute
//    public void testModelAttribute(String qq, Model model) {
//        System.out.println("qq = " + qq);
//        model.addAttribute("message", "testModelAttribute_" + qq);
//    }

    @RequestMapping("/test2")
    public String test2(String name, String qq) {


        return "simple";
    }

    @RequestMapping("/test3")
    public String test3(@ModelAttribute(value = "testItems") Items Items) {
        return "items";
    }


    // http://localhost:8080/test4?name=a1&name=a2&name=a3
    @RequestMapping("/test4")
    @ResponseBody
    public String test4(String[] name) {
        String result = Arrays.toString(name);
        return result;
    }

    @RequestMapping("/test5")
    @ResponseBody
    public String test5(Items items) {
        return items.toString();
    }


    @RequestMapping("/test6")
    @ResponseBody
    public String test6() {

        if (true) {
            throw new MyException("测试异常");
        }
        return "ok";
    }
}
